﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace intelliEssay.Core
{
    /// <summary>
    /// 任务池
    /// <remarks>
    /// 任务池记录自intelliEssay启动以来进行的所有任务</remarks>
    /// </summary>
    public static class TaskPool
    {
        internal static List<Task> TasksInPool = new List<Task>();
        /// <summary>
        /// 依据任务类型获得所有和该任务类型相同的任务
        /// </summary>
        /// <param name="TaskType"></param>
        /// <returns></returns>
        public static IEnumerable<Task> GetTask(string TaskType)
        {

            return TasksInPool.Where<Task>(o =>
                {
                    return o.taskType == TaskType;
                });
        }
        /// <summary>
        /// 和GetTask(tsk.TaskType)相同
        /// </summary>
        /// <param name="tsk"></param>
        /// <returns></returns>
        public static IEnumerable<Task> GetTask(Task tsk)
        {

            return GetTask(tsk.taskType);
        }
        /// <summary>
        /// Kills all tasks in the taskPool
        /// </summary>
        public static void Kill()
        {
            System.Threading.Tasks.Parallel.ForEach<Task>(TasksInPool, new Action<Task>(o =>
                {
                    o.Kill();
                }));
        }
        /// <summary>
        /// Kills a group of task, based on taskType
        /// </summary>
        /// <param name="taskType"></param>
        public static void Kill(string taskType)
        {
            System.Threading.Tasks.Parallel.ForEach<Task>(GetTask(taskType), new Action<Task>(o =>
            {
                o.Kill();
            }));
        }
    }
}
